﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace PLCWpfTest.Controls
{
    /// <summary>
    /// THDashboard.xaml 的交互逻辑
    /// </summary>
    public partial class THDashboard : UserControl
    {
        private static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(THDashboard), new PropertyMetadata(string.Empty, null, TitleCoerceValueCallback));

        private static readonly DependencyProperty TValueProperty = DependencyProperty.Register(nameof(TValue), typeof(double), typeof(THDashboard), new PropertyMetadata(0.0, TValuePropertyChangedCallback, null));

        private static readonly DependencyProperty HValueProperty = DependencyProperty.Register(nameof(HValue), typeof(double), typeof(THDashboard), new PropertyMetadata(0.0, HValuePropertyChangedCallback, null));

        private static readonly DependencyProperty ModuleErrorProperty = DependencyProperty.Register(nameof(ModuleError), typeof(bool), typeof(THDashboard), new PropertyMetadata(false, ModuleErrorPropertyChangedCallback, null));

        [Bindable(true), Category("自定义属性"), Description("温湿度仪表盘标题")]
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("温度值")]
        public double TValue
        {
            get { return (double)GetValue(TValueProperty); }
            set { SetValue(TValueProperty, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("湿度值")]
        public double HValue
        {
            get { return (double)GetValue(HValueProperty); }
            set { SetValue(HValueProperty, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("模块是否错误")]
        public bool ModuleError
        {
            get { return (bool)GetValue(ModuleErrorProperty); }
            set { SetValue(ModuleErrorProperty, value); }
        }

        public THDashboard()
        {
            InitializeComponent();
        }

        private static object TitleCoerceValueCallback(DependencyObject d, object baseValue)
        {
            if (baseValue == null)
            {
                return baseValue;
            }
            THDashboard thDashboard = (THDashboard)d;
            string value = baseValue.ToString();
            thDashboard.TitleLabel.Content = value;
            return value;
        }

        private static void TValuePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            THDashboard thDashboard = (THDashboard)d;
            double value = (double)e.NewValue;
            thDashboard.MyDoubleDashboard.OneInnerRingValue = value;
            thDashboard.TValueLabel.Content = value.ToString();
        }

        private static void HValuePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            THDashboard thDashboard = (THDashboard)d;
            double value = (double)e.NewValue;
            thDashboard.MyDoubleDashboard.TwoInnerRingValue = value;
            thDashboard.HValueLabel.Content = value.ToString();
        }

        private static void ModuleErrorPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            THDashboard thDashboard = (THDashboard)d;
            bool moduleError = (bool)e.NewValue;
            Color color = moduleError ? Colors.Red : (Color)thDashboard.FindResource("HighlightBackground");
            thDashboard.TitleLabel.Background = new SolidColorBrush(color);
        }
    }
}